package com.why;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.why.bean.User;
import com.why.service.LoginService;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException {

        test1();

//        String str = "{\n" +
//                "\t\"type\": \"login\",\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"userName\": \"why\",\n" +
//                "\t\t\"password\": \"123456.0\"\n" +
//                "\t}\n" +
//                "}";
//        StringBuffer sb = new StringBuffer();
//        sb.append(str);
//        System.out.println(response(sb));
    }

    public static void test1() throws IOException {
        while (true){
            ServerSocket serverSocket=new ServerSocket(8889);
            Socket scoket = serverSocket.accept();
            InputStream is = scoket.getInputStream();
            StringBuffer  request = new StringBuffer();
            byte [] b=new byte[1024];
            int len=0;
            while ((len=is.read(b))!=-1){
                request.append(new String(b, StandardCharsets.UTF_8).trim());
            }
            String response = response(request);
            OutputStream os = scoket.getOutputStream();
            os.write(response.getBytes("UTF-8"));
            scoket.close();
            serverSocket.close();
        }

    }

    private static String response(StringBuffer request) {
        String result = "";
        System.out.println("result:" + request.toString());
        JSONObject json = JSON.parseObject(request.toString());
        String type = json.getString("type");
            if(type.equals("login")){
                User user = JSON.toJavaObject(json.getJSONObject("data"),User.class);
                System.out.println(user.getUserName());
                System.out.println(user.getPassword());
                Boolean flag = LoginService.login(user.getUserName(),user.getPassword());
                if(flag){
                    result = "登录成功";
                }else{
                    result = "登录失败，用户名或者账号错误";
                }
            }
        return result;
    }

    public static void test2() throws IOException {
        ServerSocket ss= new ServerSocket(20000);
        InputStream inputStream = null;
        StringBuffer request = new StringBuffer();
        byte[] bytes = new byte[1024];
        Socket s = ss.accept();
        inputStream = s.getInputStream();
        while (inputStream.read(bytes) != -1) {
            // 使用.trim()去掉空字符，不然byte[]里面会包含很多空字符
            request.append(new String(bytes, StandardCharsets.UTF_8).trim());
        }
        System.out.println("客户端发送的请求是：" + request.toString());
        OutputStream os = s.getOutputStream();
        os.write("您好，服务器收到了你的请求！".getBytes("UTF-8"));
        os.close();
        inputStream.close();
        s.close();
        System.out.println("running... ");
    }
}
